<template>
  <div>
    <!-- 底部部分 -->
    <footer class="footer" v-show="list.length">
      <span class="todo-count"
        ><strong>{{ num }}</strong
        >剩余</span
      >
      <ul class="filters">
        <li>
          <a
            :class="{ selected: border === 'all' }"
            href="#/"
            @click="filter('all')"
            >全部</a
          >
        </li>
        <li>
          <a
            :class="{ selected: border === 'go' }"
            href="#/active"
            @click="filter('go')"
            >进行中</a
          >
        </li>
        <li>
          <a
            :class="{ selected: border === 'accom' }"
            href="#/completed"
            @click="filter('accom')"
            >已完成</a
          >
        </li>
      </ul>
      <button class="clear-completed" @click="clear" v-show="hide">
        清除已完成
      </button>
    </footer>
  </div>
</template>

<script>
export default {
  props: {
    list: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      border: "all",
    };
  },
  methods: {
    clear() {
      this.$emit("clear");
    },
    filter(border) {
      this.border = border;
      if (this.border === "all") {
        this.list.forEach((item) => {
          item.show = true;
        });
      } else if (this.border === "go") {
        this.list.forEach((item) => {
          item.show = !item.isDone;
        });
      } else if (this.border === "accom") {
        this.list.forEach((item) => {
          item.show = item.isDone;
        });
      }
    },
  },
  computed: {
    num() {
      //   let a = 0;
      //   this.list.forEach((item) => {
      //     if (!item.isDone) {
      //       a++;
      //     }
      //   });
      //   return a;
      return this.list.filter((item) => item.isDone === false).length;
    },
    hide() {
      return this.list.some((item) => {
        return item.isDone === true;
      });
    },
  },
};
</script>

<style>
</style>